<?php

namespace App\Admin\Controllers\Combination;

use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use App\Admin\Repositories\CmcPromotionPintuan;
use App\Models\coupon\CmcPromotionCoupon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB as CmcGoods;

class CmcPromotionPintuanController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CmcPromotionPintuan(), function (Grid $grid) {
            $grid->column('pintuan_id','ID')->sortable();
            $grid->column('pintuan_name')->editable(true);
            $grid->column('goods_name','商品名称');
            $grid->column('is_virtual_goods', '是否虚拟')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'不虚拟',
                    1=>'虚拟'
                ])
            );
            $grid->column('pintuan_image','拼团图片')->image($server=null,80,80);
            $grid->column('group_count','拼团组数量');
            $grid->column('status', '拼团状态')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'未开启',
                    1=>'开启'
                ])
            );
            $grid->column('pintuan_num');
            $grid->column('pintuan_time');
            $grid->column('remark');
            $grid->column('is_recommend', '是否推荐')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'不推荐',
                    1=>'推荐'
                ])
            );
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->column('buy_num');
            $grid->column('pintuan_price');
            $grid->column('is_virtual_buy');
            $grid->column('is_single_buy');
            $grid->column('is_promotion');
            $grid->column('group_num');
            $grid->column('order_num');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();
            // 倒序排序
            $grid->model()->orderBy('created_at', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('goods_name','商品名称');
                $filter->equal('status','拼团状态');
            });
            // 回收站
            $grid->filter(function ($filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\Combination\CmcPromotionPintuan::class));
                }
            });
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\Combination\CmcPromotionPintuan::class));
                }
            });
            // 异步渲染功能
            $grid->async();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcPromotionPintuan(), function (Show $show) {
            $show->field('pintuan_id');
            $show->field('pintuan_name');
            $show->field('goods_id');
            $show->field('is_virtual_goods');
            $show->field('pintuan_num');
            $show->field('pintuan_time');
            $show->field('remark');
            $show->field('is_recommend');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('buy_num');
            $show->field('pintuan_price');
            $show->field('is_virtual_buy');
            $show->field('is_single_buy');
            $show->field('is_promotion');
            $show->field('group_num');
            $show->field('order_num');
            $show->field('pintuan_image');
            $show->field('group_count');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CmcPromotionPintuan(), function (Form $form) {
            $form->display('pintuan_id');
            $form->text('pintuan_name')->rules('required');
            $form->select('goods_name')->options(
                CmcGoods::table('cmc_goods')->where('goods_state',1)->pluck('goods_name','goods_name')
            );
            $form->text('is_virtual_goods')->rules('required');
            $form->text('pintuan_num')->rules('required');
            $form->datetime('pintuan_time')->rules('required');
            $form->textarea('remark')->rules('required');
            $form->text('is_recommend')->rules('required');
            $form->datetime('start_time')->rules('required');
            $form->datetime('end_time')->rules('required');
            $form->text('buy_num')->rules('required');
            $form->text('pintuan_price')->rules('required');
            $form->text('is_virtual_buy')->rules('required');
            $form->text('is_single_buy')->rules('required');
            $form->text('is_promotion')->rules('required');
            $form->text('group_num')->rules('required');
            $form->text('order_num')->rules('required');
            $form->image('pintuan_image')->rules('required');
            $form->text('group_count')->rules('required');
            $form->text('status')->rules('required');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
